package com.common
{
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;

	/**
	 * @author lijia
	 */	
	public class FileUtil
	{
		public function FileUtil()
		{
		}

		/**
		 * 打开文件
		 * @param callBack
		 */		
		public static function openSelectFile(callBack:Function = null, path:String = ""):void{
			var fileToOpen:File=path?new File(path):new File();
			
			fileToOpen.browseForOpen("选择文件");
			fileToOpen.addEventListener(Event.SELECT, function(e:Event):void{
				var file:File=e.target as File;
				if(callBack!=null){
					callBack(file);
				}
			});
		}
		
		/**
		 * 打开一个目录
		 */		
		public static function openFilePath(callBack:Function = null):void{
			var fileToOpen:File=new File();
			fileToOpen.browseForDirectory("选择目录");
			fileToOpen.addEventListener(Event.SELECT, function(e:Event):void{
				var file:File=e.target as File;
				if(callBack!=null){
					callBack(file);
				}
			});
		}
		
		public static function saveFile(fileData:ByteArray, filePath:String):void{
			var fs:FileStream = new FileStream();
			fs.open(new File(filePath), FileMode.WRITE);
			fs.writeBytes(fileData);
			fs.close();
		}
		
		public static function saveTextFile(file:File, str:String, charSet:String):void{
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.WRITE);
			fs.writeMultiByte(str, charSet);
			fs.close();
		}
		
	}
}